<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
		"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>dojox.html.set test</title>
	<script >
	function fixPngIE6(){
		if(this.complete && dojo.isIE < 7){
			var r = this.runtimeStyle;
			if(/.png$/i.test(this.src)){
				r.height = this.height;
				r.width = this.width;
				r.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.src+"');";
				this.src = this.currentStyle.backgroundImage.replace(/url\(\s*['"]?(.+?)['"]?\s*\)/, "$1");
			}
			this.className = this.className.replace('run_png_fix', "");
			r.behaviour = 'none';
		}
	}
	</script>
	<style type='text/css'>
		.run_png_fix {
			background-image:url(images/blank.gif);
			behaviour: expression(fixPngIE6.call(this));
		}
	</style>
	<script src='../../../dojo/dojo.js' djConfig='isDebug:true, parseOnLoad:true'></script>
	<script src='../../../dojo/html.js'></script>
	<script src='../../../dojox/html/_base.js'></script>
	<script>
		dojo.require('doh.runner');
		dojo.require('dojox.html');
		dojo.require('dojo.parser');

		// used to test if we fire scrips to document scope
		function documentCallback(){
			arguments.callee.reached = true;
			//console.debug('reached');
		}
		var unTypedVarInDocScope; // a closure test to make sure we can reach this from evaled scripts

		var node1, setter1, setter2;

		dojo.addOnLoad(function(){
			/* 
				These tests are largely borrowed from dojox.layout.ContentPane's testsuite
				They provide coverage for the basic use of the:
				 adjustPaths, renderStyles, executeScripts and referencePath properties
				
				We also need to test a couple other scenarios that dojox.html.set allows for: 
				Reuse of a Setter across different node contexts
				
			
			*/

			node1 = dojo.byId('node1');
			setter1 = new dojox.html._ContentSetter({
				node: node1
			});

			function ieTrimSpaceBetweenTags(str){
				return str.replace(/(<[a-z]*[^>]*>)\s*/ig, "$1");
			}

			doh.register("basicChecks", [
					{
						name: 'setContent',
						runTest: function(t){
							// test that setContent on a simple node does in fact set it
							console.log("basicChecks: " + this.name);
							var msg = "Simple Test";
							
							dojox.html.set(node1, msg);
							t.assertEqual(msg, node1.innerHTML);
							console.log("/basicChecks: " + this.name);
						}
					}
				]
			);

			doh.register("pathAdjustments",
				[
					{
						setUp: function() {
							// setup text fixture for these pathAdjustments tests
							// we use a _Setter instance rather than dojox.html.set
							// as the tests include successive sets and depend on prior settings and results
							setter2 = new dojox.html._ContentSetter({ node: node1 });
							
						},
						name: 'cssPathAdjustments',
						runTest: function(t){
							// test that when you setContent, using the adjustPaths and renderStyles options
							// the paths in the css are corrected for the current document
							console.log("pathAdjustments: " + this.name);

							// we do this test as one big string to emulate as good as possible, 
							// but split it later to easily see where we failed
							var cssText = ".easy{ background-image:url(images/image.png) }\n"
							+".dontReplaceEasy{ background-image:url(images/images/image.png) }\n"
							+".hardurl{background-image:url(\t \"../../source/~test/%20t'e(s)t.gif(\"1')?foo=bar11103&bar=baz-foo\"  \t);}body{};\n"
							+".file{background-image: url(file:///home/nobody/image.png);}\n"
							+".http{background-image: url(http://dojotoolkit.org/image.png);}\n"
							+".https{background-image: url(https://dojotoolkit.org/image.png);}\n"
							+".nonRelative{background-image:url(/toplevelfolder/image.gif);}\n"
							+'@import "css/main.css";' + "\n@import \t'css/Newbee Url.css'\t;\n"
							+"@import 'http://dojotoolkit.org/dojo.css';\n"
							+"  @import 'you/never/thought/' print;\n"
							+' @import url("it/would/work") tv, screen;'+"\n"
							+' @import url(/did/you/now.css);'+"\n"
							+' @import "yes.i.did";';
							
							var setParams = {
								referencePath: "deep/nested/file",
								adjustPaths: 1,
								renderStyles: 1
							};
							
							var adjustedCss;

							// hijack internals to snatch the styles before they are inserted to DOM (DOM messes formating)
							var oldFunc = setter2._renderStyles;
							console.log("in cssPathAdjustments, returning function");

							setter2._renderStyles = function(styles){
								adjustedCss = styles.join();
							};
							console.log("in cssPathAdjustments, calling set");
							setter2.set('<style>'+cssText+'</style>', setParams);
							console.log("in cssPathAdjustments, done calling set");
							
							setter2._renderStyles = oldFunc;

							adjustedCss = adjustedCss.split("\n");

							var expectedCss = (".easy{ background-image:url(deep/nested/images/image.png) }\n"
							+".dontReplaceEasy{ background-image:url(deep/nested/images/images/image.png) }\n"
							+".hardurl{background-image:url(source/~test/%20t'e(s)t.gif(\"1')?foo=bar11103&bar=baz-foo);}body{};\n"
							+".file{background-image: url(file:///home/nobody/image.png);}\n"
							+".http{background-image: url(http://dojotoolkit.org/image.png);}\n"
							+".https{background-image: url(https://dojotoolkit.org/image.png);}\n"
							+".nonRelative{background-image:url(/toplevelfolder/image.gif);}\n"
							+"@import \"deep/nested/css/main.css\";\n@import \"deep/nested/css/Newbee Url.css\"\t;\n"
							+"@import 'http://dojotoolkit.org/dojo.css';\n"
							+"  @import \"deep/nested/you/never/thought/\" print;\n"
							+' @import url(deep/nested/it/would/work) tv, screen;'+"\n"
							+' @import url(/did/you/now.css);'+"\n"
							+' @import "deep/nested/yes.i.did";').split("\n");

							// we split and loop to get a faster hint of where it failed
							for(var i = 0; i < expectedCss.length; i++){
								t.assertEqual(expectedCss[i], adjustedCss[i]);
							}
						},
						tearDown: function(){
							delete setter2.adjustPaths; // get back to defaults
							delete setter2.renderStyles;
						}
					},
					{
						name: 'htmlPathAdjustments',
						timeout: 1800,
						runTest: function(t){
							console.log("pathAdjustments: " + this.name);
							var d = new t.Deferred();
							setTimeout(d.getTestCallback(
								function(){
									console.log("pathAdjustments: htmlPathAdjustments, in callback");
									console.log("executeScripts: %s, cleanContent: %s, referencePath: %s, renderStyles: %s", setter2.executeScripts, setter2.cleanContent, setter2.referencePath, setter2.renderStyles);
									// check that images and styles have been applied
									var cb = dojo.contentBox(dojo.byId('imgTest'));
											//dojo.getComputedStyle(dojo.byId('imgTest'));
									t.assertEqual(188, cb.w);
									t.assertEqual(125, cb.h);

									// make sure we didn't mess up the other inline styles
									cb = dojo.contentBox(dojo.byId('inlineStyleTest'));
									t.assertEqual(188, cb.w);
									t.assertEqual(125, cb.h);

									// make sure it is the correct image
									var cs = dojo.getComputedStyle(dojo.byId('inlineStyleTest'));
									var url = cs.backgroundImage;
									//remove url(..)
									url = url.replace(/^\s?url\(['"]?/, "").replace(/['"]?\);?\s?$/, ""); 
									// compare image url to full path of this document
									imageUrl = dojo.moduleUrl('dojox', 'html/tests/images/testImage.gif');
									console.log(new dojo._Url(document.location, imageUrl).toString(), url);
									t.assertEqual(new dojo._Url(document.location, imageUrl).toString(), url);

									// make sure we loaded the <link rel='stylesheet' correctly
									var mb = dojo.marginBox(dojo.byId('linkCssTest'));
									t.assertEqual(112, mb.w); // 100px  + 2px border + 4px margin = 112px
									t.assertEqual(112, mb.h);

									// make sure we loaded the <style>@import '...'; correctly
									mb = dojo.marginBox(dojo.byId('importCssTest'));
									t.assertEqual(110, mb.w); // 100px + 1px border + 4px margin = 110px
									t.assertEqual(110, mb.h);

									// make sure we didn't render the <link media='print' rel='stylesheet'
									var mb = dojo.marginBox(dojo.byId('linkMediaTest'));
									t.assertEqual(212, mb.w); // 100px  + 2px border + 4px margin = 112px
									t.assertEqual(212, mb.h);

									// make sure we didn't render the <style media='print'>@import '...';
									mb = dojo.marginBox(dojo.byId('importMediaTest'));
									t.assertEqual(210, mb.w); // 100px + 1px border + 4px margin = 110px
									t.assertEqual(210, mb.h);
									console.log("/pathAdjustments: htmlPathAdjustments, in callback");
								}
							), 1500);

							console.log("pathAdjustments: " + this.name + ": requesting content");
							var remoteUrl = dojo.moduleUrl("dojox", 'html/tests/remote/getResponse.php?mode=htmlPaths'); 
							
							dojo.xhrGet({
								url: remoteUrl, 
								load: function(data) {
									console.log("pathAdjustments: htmlPathAdjustments, handling response");
									setter2.set(data, {
										adjustPaths: 1, 
										referencePath: remoteUrl.toString(),
										renderStyles: 1
									}); 
									console.log("/pathAdjustments: htmlPathAdjustments, handling response");
								}
							});
							return d;
						},
						tearDown: function(){
							delete setter2.adjustPaths; // get back to defaults
							delete setter2.renderStyles;
						}
					},
					{
						name: 'renderStylesOfByDefaultAndOldDeleted',
						timeout: 1800,
						runTest: function(t){
							var d = new t.Deferred();
							console.log("pathAdjustments: " + this.name);

							setTimeout(d.getTestCallback(
								function(){
									// innerHTML'ing <link tags works in some browser (IE, moz), but not all
									// we can't test if LINK was loaded this way

									// make sure we didn't load the <link rel='stylesheet'
									//var mb = dojo.marginBox(dojo.byId('linkCssTest'));
									//t.assertFalse(112 == mb.w);
									//t.assertFalse(112 == mb.h);

									// make sure we didn't load the <style>@import '...';
									var mb = dojo.marginBox(dojo.byId('importCssTest'));
									t.assertFalse(110 == mb.w);
									t.assertFalse(110 == mb.h);
								}
							), 1500);

							var remoteUrl = dojo.moduleUrl("dojox", 'html/tests/remote/getResponse.php?mode=htmlPaths'); 
							dojo.xhrGet({
								url: remoteUrl, 
								load: function(data) {
									console.log("pathAdjustments: renderStylesOfByDefaultAndOldDeleted, handling response");
									setter2.set(data, {
										adjustPaths: 1, 
										referencePath: remoteUrl.toString()
									}); 
									console.log("/pathAdjustments: htmlPathAdjustments, handling response");
								}
							});
							return d;
						},
						tearDown: function(){
							delete setter2.adjustPaths;
						}
					}
				]
			);

			doh.register("scriptTests",
				[
					{
						name: 'leaveDojoMethodScriptsAsIs',
						runTest: function(t){
							console.log("scriptTests: " + this.name);
							dojox.html.set(node1, 
								"<"
								+"script type='dojo/method'>unTypedVarInDocScope = 'failure';<"
								+"/script>", 
								{
									executeScripts: true
								}
							);

							var d = new t.Deferred();
							// IE req to async this test
							setTimeout(d.getTestCallback(function(){
								t.assertEqual('undefined', typeof unTypedVarInDocScope);
								t.assertFalse(unTypedVarInDocScope == 'failure');
							}), 40);

							return d;
						}
					},
					{
						name: 'scripts_evals_in_global_scope',
						timeout: 1800, // grabing remote js, wait for that
						runTest: function(t){
							console.log("scriptTests: " + this.name);
							var remoteUrl = dojo.moduleUrl("dojox", "html/tests/remote/getResponse.php?mode=remoteJsTrue");
							dojox.html.set(
								node1, 
								"<"
								+"script>function scriptsInGlobalClicked(){ documentCallback(); }<"
								+"/script><"+"script src='" + remoteUrl +"'></"
								+"script>"+"<a href='javascript:scriptsInGlobalClicked()' "
									+"onfocus='scriptsInGlobalClicked();' id='anchorTag'>test</a>", 
								{
									executeScripts: true
								}
							);

							var link = dojo.byId('anchorTag');
							dojo.isFunction(link.click) ? /*others*/ link.click() : /*moz*/ link.focus();
							var d = new t.Deferred();
					
							setTimeout(d.getTestCallback(function(){
								t.assertEqual('boolean', typeof documentCallback.reached);
								t.assertTrue(documentCallback.reached);
								t.assertTrue(unTypedVarInDocScope);
							}), 40);
							return d;
						}
					},
					{
						name:'scriptsEvalsInOrder',
						timeout: 1800,// grabing remote js, wait for that
						runTest: function(t){
							console.log("scriptTests: " + this.name);
							var remoteUrl = dojo.moduleUrl("dojox", "html/tests/remote/getResponse.php?mode=remoteJsFalse");
							dojox.html.set(node1, "<"
								+"script src='"+ remoteUrl +"'><"
								+"/script><"+"script>unTypedVarInDocScope = 1;<"
								+"/script>", 
								{
									executeScripts: true
								}); // scripts only test

							// we need to make this async because of IEs strange events loops
							var d = new t.Deferred();
							setTimeout(d.getTestCallback(function(){
								t.assertEqual('number', typeof unTypedVarInDocScope);
								t.assertEqual(1, unTypedVarInDocScope);
							}), 40);
							return d;
						}
					},
					{
						name: 'scriptsWithTypeTextJavascript',
						runTest: function(t){
							console.log("scriptTests: " + this.name);
							dojox.html.set(node1, "<"
								+"script type='text/javascript'> unTypedVarInDocScope = 'text/javascript'; <"
								+"/script>", {
									executeScripts: true
								}
							);

							var d = new t.Deferred();
							// IE needs async here
							setTimeout(d.getTestCallback(function(){
								t.assertEqual('text/javascript', unTypedVarInDocScope);
							}), 40);
							return d;
						}
					},
					{
						name:'scriptsWithHtmlComments',
						runTest: function(t){
							dojox.html.set(node1, "<"
								+"script><!-- unTypedVarInDocScope = 2; --><"
								+"/script>", {
									cleanContent: 1,
									executeScripts: 1 
								}
							);

							var d = new t.Deferred();
							// IE need a async here
							setTimeout(d.getTestCallback(function(){
								t.assertEqual('number', typeof unTypedVarInDocScope);
								t.assertEqual(2, unTypedVarInDocScope);
							}), 40);

							return d;
						},
						tearDown: function(){
							node1.innerHTML = "";
						}
					},
					{
						name:'scriptsWithCData',
						runTest: function(t){
							dojox.html.set(node1, "<"
								+"script><![CDATA[ unTypedVarInDocScope = 3; ]]><"
								+"/script>", {
									cleanContent: 1,
									executeScripts: 1 
								}
							);

							var d = new t.Deferred();
							// IE need a async here
							setTimeout(d.getTestCallback(function(){
								t.assertEqual('number', typeof unTypedVarInDocScope);
								t.assertEqual(3, unTypedVarInDocScope);
							}), 40);

							return d;
						},
						tearDown: function(){
							node1.innerHTML = "";
						}
					},
					// FIXME: what /should/ container be in this context? 
					{
						name: 'replace_container_',
						runTest: function(t){
							unTypedVarInDocScope = 'failure';
							dojox.html.set(node1, 
								"<"
								+"script>function testReplace(){"
								+	"if(typeof _container_ != 'object'){return 'not replaced 1';}\n"
								+	"if(_container_ != setter1){ return 'not replaced 2';}\n"
								+	"if(!_container_ == setter1){ return 'not replaced 3';}\n"
								+	"var tmp =_container_=dojo;\n"
								+	"if(tmp != dojo){ return 'replaced when shouldnt 1';}\n"
								+	"var tmp = _container_  \t \t = dojo;\n"
								+	"if(tmp != dojo){ return 'replaced when shouldnt 2';}\n"
								+	"return 'success';\n"
								+"};\n"
								+"unTypedVarInDocScope = testReplace();"
								+"</"+"script>", {
									executeScripts: 1, 
									scriptHasHooks: true, 
									scriptHookReplacement: function() { return "setter1"; }
							});
							
							// let IE inhale here
							var d = new t.Deferred();
							setTimeout(d.getTestCallback(function(){
								t.assertEqual('success', unTypedVarInDocScope);
							}), 40);
							return d;
						},
						tearDown: function(){
							node1.innerHTML = "";
							unTypedVarInDocScope = "done";
						}
					},
					
					"t.assertEqual('done', unTypedVarInDocScope)",
					
					{
						name: 'evalThenParse',
						runTest: function(t){
							dojo.global._evalThenParseResult = "";
							// test the executeScripts / parse sequence
							// by declaring a class that when instantiated returns true
							dojox.html.set(node1, 
								"<"+"script>"
								+"_evalThenParseResult+='a';\n"
								+"dojo.provide('dojox.html.tests.TestThinger');"
								+"dojo.declare('dojox.html.tests.TestThinger', null, {"
								+"  constructor: function() {"
								+"	 _evalThenParseResult +='b';"
								+"  }"
								+"});"
								+"</"+"script>\n"
								+""
								+"<div dojoType='dojox.html.tests.TestThinger'>\n"
									+"<"+"script type='dojo/method'>dojo.global._evalThenParseResult +='c';</"+"script>\n"
								+"</div>\n"
								, {
									executeScripts: 1, 
									scriptHasHooks: false,
									parseContent: true
							});
							
							t.assertEqual('abc', dojo.global._evalThenParseResult);
						},
						tearDown: function(){
							delete dojo.global._evalThenParseResult;
						}
					}
				]
			);


			doh.register('regexRegressionAndSpeedtest',[
				{
					name: 'cssPathAdjustments',
					runTest: function(t){
						// we do this test as one big string to emulate as good as possible, 
						// but split it later to easily see where we failed
						var cssText = ".easy{ background-image:url(images/image.png) }\n"
						+".dontReplaceEasy{ background-image:url(images/images/image.png) }\n"
						+".hardurl{background-image:url(\t \"../../source/~test/%20t'e(s)t.gif(\"1')?foo=bar11103&bar=baz-foo\"  \t);}body{};\n"
						+".file{background-image: url(file:///home/nobody/image.png);}\n"
						+".http{background-image: url(http://dojotoolkit.org/image.png);}\n"
						+".https{background-image: url(https://dojotoolkit.org/image.png);}\n"
						+".nonRelative{background-image:url(/toplevelfolder/image.gif);}\n"
						+'@import "css/main.css";' + "\n@import \t'css/Newbee Url.css'\t;\n"
						+"@import 'http://dojotoolkit.org/dojo.css';\n"
						+"  @import 'you/never/thought/' print;\n"
						+' @import url("it/would/work") tv, screen;'+"\n"
						+' @import url(/did/you/now.css);'+"\n"
						+' @import "yes.i.did";';

						var expectedCss = ".easy{ background-image:url(deep/nested/images/image.png) }\n"
						+".dontReplaceEasy{ background-image:url(deep/nested/images/images/image.png) }\n"
						+".hardurl{background-image:url(source/~test/%20t'e(s)t.gif(\"1')?foo=bar11103&bar=baz-foo);}body{};\n"
						+".file{background-image: url(file:///home/nobody/image.png);}\n"
						+".http{background-image: url(http://dojotoolkit.org/image.png);}\n"
						+".https{background-image: url(https://dojotoolkit.org/image.png);}\n"
						+".nonRelative{background-image:url(/toplevelfolder/image.gif);}\n"
						+"@import \"deep/nested/css/main.css\";\n@import \"deep/nested/css/Newbee Url.css\"\t;\n"
						+"@import 'http://dojotoolkit.org/dojo.css';\n"
						+"  @import \"deep/nested/you/never/thought/\" print;\n"
						+' @import url(deep/nested/it/would/work) tv, screen;'+"\n"
						+' @import url(/did/you/now.css);'+"\n"
						+' @import "deep/nested/yes.i.did";';

						for(var i = 0; i < 6; i++){
							cssText += cssText;
							expectedCss += expectedCss;
						}

						expectedCss = expectedCss.split("\n");

						var setParams = {
							referencePath: "deep/nested/file",
							adjustPaths:1,
							renderStyles:1
						};
						
						var adjustedCss;

						// hijack internals to snatch the styles before they are inserted to DOM (DOM messes formating)
						var oldFunc = dojox.html._ContentSetter.prototype._renderStyles;
						dojox.html._ContentSetter.prototype._renderStyles = function(styles){
							adjustedCss = styles.join();
						};

						var start = new Date();
						dojox.html.set(node1, '<style>'+cssText+'</style>', setParams);
						var end = new Date();
						
						dojox.html._ContentSetter.prototype._renderStyles = oldFunc;

						adjustedCss = adjustedCss.split("\n");
						console.info('Time used to regex scan css and adjust relative paths within css:'+
								(end - start)+' ms on '+ cssText.split('\n').length
								+' css rows, with '+ cssText.length+' characters (roughly '
								+Math.round(cssText.length/1024)+ 'Kb) of infile css');

						// we split and loop to get a faster hint of where it failed
						for(var i = 0; i < expectedCss.length; i++){
							t.assertEqual(expectedCss[i], adjustedCss[i]);
						}
					},
					tearDown: function(){
					}
				}
				,
				{
					name:'htmlPathsSpeedTest',
					runTest: function(t){
						var htmlText = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n"
							+"<title>should be removed</title>\n"
							+"<img src=\"images/image.gif\"/>Testing\n"
							+"<a href=\"../../topmost.html\">\n"
							+"	<img src=\"/siteroot/top.html\">\n"
							+"	<p style='background:url(\"images/back.png\")'>\n"
							+"	testing link\n"
							+"</p></a>\n"
							+"<style \ntype='text/css'>\n"
							+"	@import 'http://dojotoolkit.org/visible.css' tv, screen;\n"
							+"	@import \"./audio.css\" audio;\n"
							+"	@import url(/topsite/css/main.css);\n"
							+"	div.mywidget, #someId {\n"
							+"		background-color:url(../../css/main.css);"
							+"		display:none;\n"
							+"		background:url(../tmp/css)\n"
							+"	}\n"
							+"</style>\n"
							+"<link rel=\"stylesheet\" href=\"../../css/theme.css\" media=\"all\">\n"
							+"<link media='print' type='text/css' rel='stylesheet' href='../../css/theme2.css'>\n"
							+"<a style='display:block; background:url(/topmost/css)' href='../above'>above</a>\n"
							+"<sc"+"ript type=\"text/javascript\"\n src=\"..\\windows\\morons\"></scr"+"ipt>\n"
							+"<scr"+"ipt type=\"dojo/method\" src=\"/dont/mess/with/this\"></scr"+"ipt>\n"
							+"<scr"+"ipt src=\"/dont/mess/here/either\" type=\"dojo/method\"></scr"+"ipt>\n"
							+"<scr"+"ipt event=\"/havent/you/listened\" type=\"dojo/method\"></scr"+"ipt>\n"
							+"<scr"+"ipt>JS CODE</scr"+"ipt>\n"
							+"<a href='javascript:void(0)'>void</a>";


						var expectedHtml = "\n\n<img src=\"deep/nested/images/image.gif\"/>Testing\n"
							+"<a href=\"topmost.html\">\n"
							+"	<img src=\"/siteroot/top.html\">\n"
							+"	<p style='background:url(deep/nested/images/back.png)'>\n"
							+"	testing link\n"
							+"</p></a>\n"
							+"\n"
							+"\n\n"
							+"<a style='display:block; background:url(/topmost/css)' href='deep/above'>above</a>\n\n"
							+"<scr"+"ipt type=\"dojo/method\" src=\"/dont/mess/with/this\"></scr"+"ipt>\n"
							+"<scr"+"ipt src=\"/dont/mess/here/either\" type=\"dojo/method\"></scr"+"ipt>\n"
							+"<scr"+"ipt event=\"/havent/you/listened\" type=\"dojo/method\"></scr"+"ipt>\n\n"
							+"<a href='javascript:void(0)'>void</a>";


						var expectedCss = [
							"\n	@import 'http://dojotoolkit.org/visible.css' tv, screen;\n"
							+"	@import \"deep/nested/audio.css\" audio;\n"
							+"	@import url(/topsite/css/main.css);\n"
							+"	div.mywidget, #someId {\n"
							+"		background-color:url(css/main.css);"
							+"		display:none;\n"
							+"		background:url(deep/tmp/css)\n"
							+"	}\n", "@import \"css/theme.css\";", "@import \"css/theme2.css\";"];

						for(var i = 0; i < 6; i++){
							htmlText += htmlText;
							expectedHtml += expectedHtml;
							expectedCss = expectedCss.concat(expectedCss);
						}


						var setParams = {
							referencePath: "deep/nested/file",
							executeScripts: 1,
							adjustPaths: 1,
							renderStyles: 1,
							cleanContent: 1
						};
						
						var adjustedCss, adjustedHtml;

						// hijack internals to snatch the styles before they are inserted to DOM (DOM messes formating)
						var oldFunc = dojox.html._ContentSetter.prototype._renderStyles;
						dojox.html._ContentSetter.prototype._renderStyles = function(styles){
							adjustedCss = styles;
							this.executeScripts = 0;
						};

						var oldSetFunc = dojox.html._ContentSetter.prototype.setContent;
						dojox.html._ContentSetter.prototype.setContent = function(html){
							console.log("replaced setContent is called");
							adjustedHtml = this.content;
						};

						var oldXhr = dojo.xhrGet;
						dojo.xhrGet = function(){}; // kill script download

						var start = new Date();

						dojox.html.set(node1, htmlText, setParams);

						console.log("/calling set to clean and adjust paths");
						var end = new Date();

						// reset back to the way it was
						dojox.html._ContentSetter.prototype._renderStyles = oldFunc;
						dojox.html._ContentSetter.prototype.setContent = oldSetFunc;
						dojo.xhrGet = oldXhr;

						console.info('Time used to regex scan html/css and\n adjust relative paths (adjustPaths=true),\n copy scripts (executeScripts=true) and copy css innerText (renderStyles=true) and adjust paths in there \nTime:'+
								(end - start)+' ms on '+ htmlText.split('\n').length
								+' html rows, with '+ htmlText.length+' characters (roughly '
								+Math.round(htmlText.length/1024)+ 'Kb)');

						// we split and loop to get a faster hint of where it failed
						adjustedHtml = adjustedHtml.split("\n");
						expectedHtml = expectedHtml.split("\n");

						for(var i = 0; i < expectedHtml.length; i++){
							//console.debug(expectedHtml[i], i);
							//console.debug(adjustedHtml[i], i);
							t.assertEqual(expectedHtml[i], adjustedHtml[i]);
						}

						var exCssBlock, adjCssBlock;
						for(var i = 0; i < expectedCss.length; i++){
							t.assertEqual('string', typeof adjustedCss[i]);

							exCssBlock = expectedCss[i].split('\n');
							adjCssBlock = adjustedCss[i].split('\n');

							for(var j = 0; j < exCssBlock.length;j++){
								t.assertEqual(dojo.trim(exCssBlock[j]), dojo.trim(adjCssBlock[j]));
							}
							
						}
					},
					tearDown: function(){
					}
				},
				{
					name:'IE_AlphaImageLoader_PathAdjustments',
					runTest: function(t){
						if(!dojo.isIE){
							console.info('aborting test IE_AlphaImageLoader_PathAdjustments, you dont use IE');
							return;
						}
						var setParams = {
							adjustPaths: 1,
							renderStyles: 1,
							referencePath: "deep/"
						};

						var html = "<div style='width:10px;height:10px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=\"scale\", src=\"images/alpha(1).png\", nextProperty=\"useless\");'><!-- \n"
						+" alpha png in IE 6 --></div>\n"
						+"<style>\n"
						+"	.ie_menu_png {"
						+"		filter: \t progid:\n"
						+"			DXImageTransform.Microsoft.AlphaImageLoader(\n"
						+"			src='../midlevel/alpha(2).png')\n"
						+"	}\n"
						+" #my_transparent_png {filter: progid:DXImageTransform.Microsoft.AlphaImageLoader( src='/images/alpha(3).png') }\n"
						+" #my_transparent_png1 {filter: progid:DXImageTransform.Microsoft.AlhaImageLoader(src='http://no.se/images/alpha(4).png')}\n"
						+"</style>\n";


						var expectedHtml = "<div style='width:10px;height:10px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=\"scale\", src=\"deep/images/alpha(1).png\", nextProperty=\"useless\");'><!-- \n"
						+" alpha png in IE 6 --></div>\n\n";

						var expectedCss = "\n"
						+"	.ie_menu_png {"
						+"		filter: \t progid:\n"
						+"			DXImageTransform.Microsoft.AlphaImageLoader(\n"
						+"			src='midlevel/alpha(2).png')\n"
						+"	}\n"
						+" #my_transparent_png {filter: progid:DXImageTransform.Microsoft.AlphaImageLoader( src='/images/alpha(3).png') }\n"
						+" #my_transparent_png1 {filter: progid:DXImageTransform.Microsoft.AlhaImageLoader(src='http://no.se/images/alpha(4).png')}\n";


						for(var i = 0; i < 7; i++){
							html += html;	
							expectedHtml += expectedHtml;
							expectedCss += expectedCss;
						}

						var adjustedHtml, adjustedCss;

						// hijack internals to snatch the content
						var oldRenderStyles = dojox.html._ContentSetter.prototype._renderStyles;
						var oldSetContent = dojox.html._ContentSetter.prototype.setContent;
						dojox.html._ContentSetter.prototype._renderStyles = function(styles){ adjustedCss = styles.join(''); };
						dojox.html._ContentSetter.prototype.setContent = function(cont){ adjustedHtml = this.content; };

						var start = new Date();
						dojox.html.set(node1, html, setParams);
						var end = new Date();

						console.info('Time used to replace AlphaImageLoader(src="...") '
									+(end - start) + "ms in HTML with "+html.length
									+' characters (roughly '+(Math.round(html.length/1024))+'Kb)');

						// reset hijacked
						dojox.html._ContentSetter.prototype._renderStyles = oldRenderStyles;
						dojox.html._ContentSetter.prototype.setContent = oldSetContent;


						// split on newline and run a check on each row to help debugging
						expectedHtml = expectedHtml.split("\n");
						adjustedHtml = adjustedHtml.split("\n");
						for(var i = 0; i < expectedHtml.length; i++){
							t.assertEqual(expectedHtml[i], adjustedHtml[i]);
						}

						expectedCss = expectedCss.split("\n");
						adjustedCss = adjustedCss.split("\n");
						for(var i = 0; i < expectedCss.length; i++){
							t.assertEqual(expectedCss[i], adjustedCss[i]);
						}
						
					},
					tearDown: function(){
					}
				}
			]);

			doh.register("A_AlphaImageLoader_inAction", [{
				name:"AlphaLoaderShowHow",
				runTest:function(t){
					// IE filter alphaimageloader paths must be relative to the page
					// not to the cssFile that declares it

					// demo a much better way of "Fixing" alpha png in IE6 than inlining in html
					var html = "<img src='images/dojoLogo.png' class='run_png_fix'/>";

					var showHowHtml = "<pre >\nCode used in IE transparent png example\n"
								+"code (declared in main page, not through ContentPane)\n"
								+"&lt;script type='text/javascript'&gt;\n"
								+fixPngIE6.toString().replace(/\n\t?/g, "\n")
								+"\n&lt;/script&gt;\n"
								+"&lt;style type='text/css'&gt;\n"
								+"	.run_png_fix {\n"
								+"		background-image:url(images/blank.gif);\n"
								+"		behaviour: expression(fixPngIE6.call(this));\n"
								+"	}\n"
								+"&lt;/style&gt;\n\n...\n\nHtml feeded to ContentPane (or your main page):\n"
								+"&lt;img src='images/dojoLogo.png' class='run_png_fix'/&gt;\n</pre>";

					dojox.html.set(
						node1, 
						html+showHowHtml, {
							executeScripts: 1,
							renderStyles: 1
						}
					);
				}
			}]);

			doh.register("setterReuse", [
					{
						// FIXME: finish this test!
						name: 'renderStyles',
						setUp: function() {
							setter2 = new dojox.html._ContentSetter({
								renderStyles: true, 
								cleanContent: true,
								node: dojo.byId("node2")
							});
							setter2.set('<style>#node2 { font-size: 18px }</style>New value');
						},
						runTest: function(t){
							// test the fixture setup ok
							var node2 = dojo.byId("node2"); 
							t.assertEqual("New value", node2.innerHTML);
							t.assertEqual("18px", dojo.style(node2, "fontSize"));
							
							// grab the styleNodes in case we need to remove them later - there should be exactly 1
							// in the reuse scenario, this is what we have to do today
							var node2_styleNodes = setter2._styleNodes; 
							setter2._styleNodes = [];
							t.assertEqual(1, node2_styleNodes.length);

							// now reuse the setter on a different node
							var node3 = setter2.node = dojo.byId("node3");
							setter2.set('<style>#node3 { font-size: 24px }</style>Another New value');

							// test the old node is still good, 
							// and the new node got the correct value, style
							t.assertEqual("New value", node2.innerHTML);
							t.assertEqual("18px", dojo.style(node2, "fontSize"));

							t.assertEqual("Another New value", node3.innerHTML);
							t.assertEqual("24px", dojo.style(node3, "fontSize"));

							// test the old styleNode is still around
							t.assertEqual(1, node2_styleNodes.length);
							// test we have just one in the new collection
							t.assertEqual(1, setter2._styleNodes.length);
							
							// I guess you might want to do this...
							// remove content and associated styles from the current and previous node
							setter2.set("");
							t.assertEqual(0, setter2._styleNodes.length);
							t.assertFalse("24px" == dojo.style(node3, "fontSize"));
							
							setter2.node = node2;
							setter2._styleNodes = node2_styleNodes;
							setter2.set("");
							t.assertEqual(0, setter2._styleNodes.length);
							t.assertFalse("18px" == dojo.style(node2, "fontSize"));
						}
					}
				]
			);

			var remoteScriptsSetter;
			var previousContent;
			doh.register("remoteScripts", [
				{
					name: 'testCommentedScriptTag',
					setUp: function() {
						remoteScriptsSetter = new dojox.html._ContentSetter({
							renderStyles: true, 
							cleanContent: true,
							executeScripts: true,
							node: dojo.byId("node4")
						});
						previousContent = dojo.byId("node4").innerHTML;
					},
					tearDown: function(){
						dojo.byId("node4").innerHTML = previousContent;    
					},
					timeout: 10000,
					runTest: function(t){
						var deferred = new doh.Deferred(); 
						var xhrDef = dojo.xhrGet({
							url: "remote/commentedScript.html",
							preventCache: true,
							handleAs: "text"
						});
						xhrDef.addCallback(function(text){
							remoteScriptsSetter.set(text);
							var intv = setInterval(function(){
								if(window.__remotePaneLoaded){
									clearInterval(intv);
									window.__remotePaneLoaded = null;
									try{
										//Test that entity characters in a src url for a script 
										//are properly converted to correct form
										var node = document.getElementById("should_not_be_here"); 
										doh.assertTrue(node == null);
										deferred.callback(true);
									}catch(e){
										deferred.errback(new Error(e));
									}
								}
							}, 500);
						});
						xhrDef.addErrback(function(e){
							deferred.errback(e);
						});
						return deferred;
					}
				},
				{
					name: 'testEntityChars',
					setUp: function() {
						remoteScriptsSetter = new dojox.html._ContentSetter({
							renderStyles: true, 
							cleanContent: true,
							executeScripts: true,
							node: dojo.byId("node4")
						});
						previousContent = dojo.byId("node4").innerHTML;
					},
					tearDown: function(){
						dojo.byId("node4").innerHTML = previousContent;    
					},
					timeout: 10000,
					runTest: function(t){
						var deferred = new doh.Deferred(); 
						var xhrDef = dojo.xhrGet({
							url: "remote/commentedScript.html",
							preventCache: true,
							handleAs: "text"
						});
						xhrDef.addCallback(function(text){
							remoteScriptsSetter.set(text);
							var intv = setInterval(function(){
								if(window.__remotePaneLoaded2){
									clearInterval(intv);
									window.__remotePaneLoaded2 = null;
									try{
										//Test that entity characters in a src url for a script 
										//are properly converted to correct form
										var node = document.getElementById("should_not_be_here2"); 
										doh.assertTrue(node == null);
										deferred.callback(true);
									}catch(e){
										deferred.errback(new Error(e));
									}
								}
							}, 500);
						});
						xhrDef.addErrback(function(e){
							deferred.errback(e);
						});
						return deferred;
					}
				}
				
			]);

			doh.run();
		});
	</script>
	<style>
		@import "../../../dojo/resources/dojo.css";
		@import "../../../dijit/themes/tundra/tundra.css";
		@import "../../../dijit/tests/css/dijitTests.css";

		.box {
			border: 1px solid black;
			height: 190px;
			width: 80%;
			overflow: auto;
		}

		.red {
			color: red;
		}

		.dojoxTestWidget {
			border: 1px dashed red;
			background-color: #C0E209 ;
		}
		
		              
	</style>
</head>
<body class='tundra'>
	<h1>dojox.layout.ContentPane</h1>
	<h3>As dojox ContentPane is derived from dijit ContentPane, make sure that the dijit test passes before running this test</h3>
	<h3 class='red'>Test relies on a php page as backend, so you need php installed on your server</h3>

	<div class='box' id='node1'>
		Initial value
	</div>

	<div class='box' id='node2'>
		Initial value
	</div>

	<div class='box' id='node3'>
		Initial value
	</div>

	<div class='box' id='node4'>
		Initial value
	</div>                                        

	<table id='tableTest' class='box'>
		<thead>
			<tr>
				<td></td>
			</tr>
		</thead>
		<tbody>
			<tr>
				<td></td>
			</tr>
		<tbody>
	</table>
</body>
</html>
